﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;


public static class a0123456789
{
    public static float ToFloat(this object _strFloat)
    {
        string StrRegion = "en-US";
        string strFloat = _strFloat.ToString();
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        float number = 0;
        strFloat = strFloat.Replace(" ", "");
        if (float.TryParse(strFloat, style, _CultureInfo, out number))
        {
            return number;
        }
        strFloat = strFloat.Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (float.TryParse(strFloat, style, _CultureInfo, out number))
        {
            return number;
        }
        return number;
        //throw new FormatException();
    }

    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strInt"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static int ToInt(this object _strInt)
    {
        return (int)_strInt.ToDecimal();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strInt"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Int16 ToInt16(this object _strInt)
    {
        return (Int16)_strInt.ToDecimal();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strInt"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Int64 ToInt64(this object _strInt)
    {
        return (Int64)_strInt.ToDecimal();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Decimal ToDecimal(this object _strDecimal)
    {
        string StrRegion = "en-US";
        string strDecimal = _strDecimal.ToString();
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        decimal number = 0;
        strDecimal = strDecimal.Replace(" ", "");
        if (Decimal.TryParse(strDecimal, style, _CultureInfo, out number))
        {
            return number;
        }
        strDecimal = strDecimal.Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Decimal.TryParse(strDecimal, style, _CultureInfo, out number))
        {
            return number;
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Decimal ToDecimalRound(this object strDecimal, int? decimals)
    {
        string StrRegion = "en-US";
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        decimal number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
    return_value:
        if (decimals.HasValue)
        {
            return Math.Round(number, decimals.Value);
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Decimal ToDecimal(this object strDecimal, string StrRegion)
    {
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        decimal number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Decimal ToDecimalRound(this object strDecimal, string StrRegion, int? decimals)
    {
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        decimal number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Decimal.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
    return_value:
        if (decimals.HasValue)
        {
            return Math.Round(number, decimals.Value);
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Double ToDouble(this object strDecimal)
    {
        string StrRegion = "en-US";
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        Double number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>;
    public static Double ToDoubleRound(this object strDecimal, int? decimals)
    {
        string StrRegion = "en-US";
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        Double number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
    return_value:
        if (decimals.HasValue)
        {
            return Math.Round(number, decimals.Value);
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Double ToDouble(this object strDecimal, string StrRegion)
    {
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        Double number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            return number;
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// default: en-US
    /// vi-VN:  123.123,123
    /// en-US:  123,123.123
    /// </summary>
    /// <param name="strDecimal"></param>
    /// <param name="StrRegion"></param>
    /// <returns></returns>
    public static Double ToDoubleRound(this object strDecimal, string StrRegion, int? decimals)
    {
        CultureInfo _CultureInfo = new CultureInfo(StrRegion);
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Any;
        Double number = 0;
        strDecimal = strDecimal.ToString().Replace(" ", "");
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
        strDecimal = strDecimal.ToString().Replace(",", "#")
                          .Replace(".", ",")
                          .Replace("#", ".")
                          ;
        if (Double.TryParse(strDecimal.ToString(), style, _CultureInfo, out number))
        {
            goto return_value;
        }
    return_value:
        if (decimals.HasValue)
        {
            return Math.Round(number, decimals.Value);
        }
        return number;
        //throw new FormatException();
    }
    /// <summary>
    /// Defaul: en-US
    /// </summary>
    /// <param name="strDateTime"></param>
    /// <returns></returns>
    public static DateTime ToDateTime(this object strDateTime)
    {
        DateTime result = new DateTime(1900, 1, 1);
        string cultureNames = "en-US";
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureNames);
        if (!strDateTime.ToString().ToLower().Equals("NULL".ToLower()))
            DateTime.TryParse(strDateTime.ToString(), culture, DateTimeStyles.None, out result);
        return result;
    }
    /// <summary>
    /// vi-VN,en-US
    /// </summary>
    /// <param name="strDateTime"></param>
    /// <param name="cultureNames">vi-VN,en-US</param>
    /// <returns></returns>
    public static DateTime ToDateTime(this string strDateTime, string cultureNames)
    {
        DateTime result = new DateTime(1900, 1, 1);
        //string cultureNames = "en-US";
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureNames);
        if (!strDateTime.Equals("NULL") && !strDateTime.Equals(""))
            DateTime.TryParse(strDateTime, culture, DateTimeStyles.None, out result);
        return result;
    }
    public static bool ToBoolean(this object strBoolean)
    {
        return Convert.ToBoolean(strBoolean);
    }
}

